<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010 Gio CMS (http://www.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 */

$this->headTitle($this->TRANSLATOR->translator('category_list_page_title') . ' - ' . $this->TRANSLATOR->translator('common_admin_title', 'core'));
$this->headScript('js/gio/treetable.js');
?>
<div class="content">
	<div class="box header mb0">
		<div class="head"><div></div></div>
		<h2 class="g_a_breadcump">
			<a class="fl" href="<?php echo $this->url('core_dashboard_index'); ?>"><?php echo $this->TRANSLATOR->translator('dashboard_breadcump', 'core'); ?></a>
			<small class="icon arrow_right" style="display: block;"></small>
			<a class="fl" href="<?php echo $this->url('category_category_list'); ?>"><?php echo $this->TRANSLATOR->translator('category_breadcump'); ?></a>
			<small class="icon arrow_right" style="display: block;"></small>
			<a class="fl" href="javascript: void(0);"><?php echo $this->TRANSLATOR->translator('category_list_breadcump'); ?></a>
			<div class="clear"></div>
		</h2>
	</div>
</div>
<div class="content">
	<div class="desc">
		<div class="body">
			<div class="fl">
				<form action="<?php echo $this->url('category_category_list'); ?>" method="post" id="gCategoryCategoryList">
					<h4 class="g_a_h4"><?php echo $this->TRANSLATOR->translator('category_list_guide'); ?></h4>
					<p>
						<label for="simple_input"><?php echo $this->TRANSLATOR->translator('category_module'); ?></label>
						<?php echo Modules_Core_Services_Module::moduleSelect(array(
																			'name' => 'category[module_id]', 
																			'id' => 'module_id', 
																			'selected' =>  $this->defaultModule)); ?>
						<?php if (isset($this->errorMessages) && isset($this->errorMessages['module_id'])) : ?>
						<span class="g_a_error"><?php echo $this->TRANSLATOR->translator($this->errorMessages['module_id']); ?></span>
						<?php endif; ?>					
						<br>
					</p>
				</form>
			</div>
			<div class="fr">
				<a class="fl g_a_tip gCategoryOrderSaveButton g_a_submit_button mr5" href="javascript: void(0);" title="<?php echo $this->STRING->escape($this->TRANSLATOR->translator('category_actions_order_save_title')); ?>"></a>
				<a class="fl g_a_tip g_a_cancel_button mr5" href="javascript: history.go(-1);" title="<?php echo $this->STRING->escape($this->TRANSLATOR->translator('common_back_button', 'core')); ?>"></a>
				<?php if ($this->ACL->isAllowed('list', 'category')) : ?>
				<a class="fl g_a_tip" href="<?php echo $this->url('category_category_list'); ?>" title="<?php echo $this->STRING->escape($this->TRANSLATOR->translator('category_actions_list_title')); ?>">
					<img src="<?php echo $this->APP_RESOURCE_SERVER; ?>/templates/<?php echo $this->APP_TEMPLATE; ?>/skins/<?php echo $this->APP_SKIN; ?>/images/category_category_list_icon.png" style="padding: 0 5px 0 0;" />
				</a>
				<?php endif; ?>
				<?php if ($this->ACL->isAllowed('add', 'category')) : ?>
				<a class="fl g_a_tip" href="<?php echo $this->url('category_category_add'); ?>" title="<?php echo $this->STRING->escape($this->TRANSLATOR->translator('category_actions_add_title')); ?>">
					<img src="<?php echo $this->APP_RESOURCE_SERVER; ?>/templates/<?php echo $this->APP_TEMPLATE; ?>/skins/<?php echo $this->APP_SKIN; ?>/images/category_category_add_icon.png" style="padding: 0 5px 0 0;" />
				</a>
				<?php endif; ?>
			</div>
			<div class="g_a_line"></div>
			<!--  -->
			<table class="g_a_data_table g_a_ui_table" id="gCategoryTreeTable">
				<thead>
					<tr>
						<th class="checkbox w_1"><input type="checkbox" name="checkbox"></th>
						<th title="<?php echo $this->STRING->escape($this->TRANSLATOR->translator('category_name_description')); ?>" class="g_a_tip_top"><?php echo $this->TRANSLATOR->translator('category_name'); ?></th>
						<th title="<?php echo $this->STRING->escape($this->TRANSLATOR->translator('category_slug_description')); ?>" class="g_a_tip_top w_15"><?php echo $this->TRANSLATOR->translator('category_slug'); ?></th>
						<th title="<?php echo $this->STRING->escape($this->TRANSLATOR->translator('common_language', 'core')); ?>" class="w_10 g_a_tip_top g_a_language_flag">
							<?php echo Modules_Core_Services_Language::languageSwitcher('flag', ''); ?>
						</th>
						<th title="<?php echo $this->STRING->escape($this->TRANSLATOR->translator('category_status')); ?>" class="g_a_tip_top w_8"><?php echo $this->TRANSLATOR->translator('category_status'); ?></th>
						<th title="<?php echo $this->STRING->escape($this->TRANSLATOR->translator('category_id')); ?>" class="g_a_tip_top w_5"><?php echo $this->TRANSLATOR->translator('category_id'); ?></th>
					</tr>
				</thead>
					<?php if (isset($this->categories) && $this->categories) : ?>
				<tbody>
					<tr rel="-1" id="row_id_0">
						<td class="checkbox w_1"></td>
						<td colspan="5"><span><strong><?php echo $this->TRANSLATOR->translator('category_root'); ?></strong></span></td>
					</tr>
					<?php foreach ($this->categories as $index => $category) : ?>
					<tr rel="<?php echo $category['depth']; ?>" id="row_id_<?php echo $category['category_id']; ?>" class="row_parent_<?php echo $category['parent_id']; ?>">
						<td class="checkbox w_1"></td>
						<td>
							<div class="content row_draggable"><span><?php echo str_repeat('----', $category['depth']); ?><?php if ($category['depth'] > 0) : ?>&nbsp;<?php endif; ?></span><strong><?php echo $category['name']; ?></strong></div>
							<div class="g_a_actions buttons_demo">
								<?php if ($this->ACL->isAllowed('edit', 'category')) : ?>
								<a class="button yellow" href="<?php echo $this->url('category_category_edit', $category); ?>"><small class="icon pencil"></small><span><?php echo $this->TRANSLATOR->translator('category_actions_edit'); ?></span></a>
								<?php endif; ?>
								<?php if ($this->ACL->isAllowed('delete', 'category') && Modules_Category_Services_Category::count(array('parent_id' => $category['category_id'])) == 0) : ?>
								<a class="button red g_a_actions_ojbect" rel="delete_<?php echo $category['category_id']; ?>" href="javascript: void(0);"><small class="icon cross"></small><span><?php echo $this->TRANSLATOR->translator('category_actions_delete'); ?></span></a>
								<?php endif; ?>
								<?php if ($this->ACL->isAllowed('activate', 'category')) : ?>
									<?php if ($category['status'] == 'active') : ?>
									<a class="button green g_a_actions_ojbect" rel="deactivate_<?php echo $category['category_id']; ?>" href="javascript: void(0);"><small class="icon lock"></small><span><?php echo $this->TRANSLATOR->translator('category_actions_deactivate'); ?></span></a>
									<?php else : ?>
									<a class="button green g_a_actions_ojbect" rel="activate_<?php echo $category['category_id']; ?>" href="javascript: void(0);"><small class="icon unlock"></small><span><?php echo $this->TRANSLATOR->translator('category_actions_activate'); ?></span></a>
									<?php endif; ?>
								<?php endif; ?>
								<div class="clear"></div>
							</div>
						</td>
						<td class="w_15"><?php echo $category['slug']; ?></td>
						<td class="w_10">
						<?php if (($translations = Modules_Core_Services_Translation::translationItems($category, $this->categoryDao)) != null) : ?>
							<?php foreach ($translations as $language => $translation) : ?>
								<?php if ($translation == null && $language != $category['language']) : ?>
									<?php if (isset($translations[$this->APP_LANG])) : ?>
									<a class="g_a_ui_helper_action_add" href="<?php echo $this->url('category_category_add_localization', array('source_id' => $category['category_id'])); ?>/<?php echo $language; ?>/">&nbsp;</a>
									<?php else :?>
									<a class="g_a_ui_helper_action_add" href="<?php echo $this->url('category_category_add'); ?>">&nbsp;</a>
									<?php endif; ?>
							
								<?php elseif (($translation == null && $language == $category['language']) || ($translation != null && $translation['category_id'] == $category['category_id'])) : ?>
								<a class="g_a_ui_helper_action_edit" href="<?php echo $this->url('category_category_edit', $category); ?>/<?php echo $language; ?>/">&nbsp;</a>
								
								<?php elseif ($translation != null && $translation['category_id'] != $category['category_id']) : ?>
								<a class="g_a_ui_helper_action_edit" href="<?php echo $this->url('category_category_edit', $translation); ?>/<?php echo $language; ?>/">&nbsp;</a>
								<?php endif; ?>
							<?php endforeach; ?>
						<?php endif; ?>
						</td>
						<td class="w_8" id="gCoreCategoryStatus_<?php echo $category['category_id']; ?>">
							<?php if ($category['status'] == 'active') : ?>
								<?php echo $this->TRANSLATOR->translator('category_status_active'); ?>	
							<?php else : ?>
								<?php echo $this->TRANSLATOR->translator('category_status_inactive'); ?>
							<?php endif; ?>
						</td>
						<td class="w_5"><?php echo $category['category_id']; ?></td>
					</tr>
					<?php endforeach; ?>
				</tbody>
					<?php else : ?>
					<tr>
						<td colspan="6"><p><?php echo $this->TRANSLATOR->translator('category_actions_list_data_notfound'); ?></p></td>
					</tr>
					<?php endif; ?>
			</table>
			<!--  -->
			<?php if (isset($this->pager)) : ?>	
			<!-- Pager -->
			<div class="module pagination g_a_pager">
				<?php echo $this->pager->links; ?>
			</div>
			<!-- / Pager -->
			<?php endif; ?>	
			<p class="small">&nbsp;</p>
			<p class="last buttons_demo">
				<?php if ($this->ACL->isAllowed('add', 'category', 'category')) : ?>
				<a class="button gCategoryOrderSaveButton green" href="javascript: void(0);"><small class="icon check"></small><span><?php echo $this->TRANSLATOR->translator('category_actions_order_save_button'); ?></span></a>
				<?php endif; ?>
				<a class="button red" href="javascript: history.go(-1);"><small class="icon cross"></small><span><?php echo $this->TRANSLATOR->translator('common_back_button', 'core'); ?></span></a>
			</p>
			<div class="clear"></div>
		</div>
	</div>
	<div class="body_footer">
		<div class="bottom_left"><div class="bottom_right"></div></div>
	</div>
</div>
<script type="text/javascript">
'Gio.Core.Category'.namespace();
Gio.Core.Category.currenAction = null;
Gio.Core.Category.actions = function(params) {
	/**
	 * Show g_a_ajax_loading element
	 */
	$('#g_a_ajax_loading').show();
	$.ajaxq('category_category_action', {
		type: 'post',
		url: params.url,
		data: params,
		success: function(response) {
			if (response == 'RESULT_OK') {
				var type = 'success';
				$('#row_id_' + params.category_id).addClass('g_a_row_affected');
				switch (params.act) {
					case 'delete':
						var message = '<?php echo $this->STRING->escape($this->TRANSLATOR->translator('category_actions_delete_success')); ?>';
						break;
					case 'activate':
					case 'deactivate':
						var rel = (params.act == 'activate') ? 'deactivate_' + params.category_id : 'activate_' + params.category_id;
						var className = (params.act == 'activate') ? 'lock' : 'unlock';
						var text = (params.act == 'activate') ? '<?php echo $this->TRANSLATOR->translator('category_actions_deactivate'); ?>' : '<?php echo $this->TRANSLATOR->translator('category_actions_activate'); ?>';
						var textStatus = (params.act == 'activate') ? '<?php echo $this->TRANSLATOR->translator('category_status_active'); ?>' : '<?php echo $this->TRANSLATOR->translator('category_status_inactive'); ?>';
						var message = (params.act == 'activate') ? '<?php echo $this->TRANSLATOR->translator('category_actions_activate_success'); ?>' : '<?php echo $this->TRANSLATOR->translator('category_actions_deactivate_success'); ?>';

						$(Gio.Core.Category.currenAction).attr('rel', rel);
						$(Gio.Core.Category.currenAction).children('small:first').removeClass('lock')
														.removeClass('unlock')
														.addClass(className);
						$(Gio.Core.Category.currenAction).children('span:first').html(text);
						$('#gCoreCategoryStatus_' + params.category_id).html(textStatus);
						break;
				}
				Gio.Message.success(message);
			} else {
				Gio.Message.error('<?php echo $this->STRING->escape($this->TRANSLATOR->translator('common_error_message', 'core')); ?>');
			}
			/**
			 * Hide g_a_ajax_loading element
			 */
			$('#g_a_ajax_loading').hide();
		}
	});
};

$(document).ready(function() {
	var treeTable = new Gio.TreeTable('gCategoryTreeTable', 'row');
	/**
	 * Save button handler
	 */
	$('.gCategoryOrderSaveButton').click(function() {
		var data = $.toJSON(treeTable.getTreeData());
		var self = this;
		/**
		 * Show g_a_ajax_loading element
		 */
		$('#g_a_ajax_loading').show();
		
		$.ajaxq('category_order', {
			type: 'POST',
			url: '<?php echo $this->url('category_category_order'); ?>',
			data: { data: data },
			success: function(response) {
				if ('RESULT_OK' == response) {
					Gio.Message.success('<?php echo $this->STRING->escape($this->TRANSLATOR->translator('category_actions_order_save_success')); ?>');
				} else {
					Gio.Message.error('<?php echo $this->STRING->escape($this->TRANSLATOR->translator('common_error_message', 'core')); ?>');
				}
				/**
				 * Show g_a_ajax_loading element
				 */
				$('#g_a_ajax_loading').hide();
			}
		});
	});
	
	$('.g_a_actions .g_a_actions_ojbect').each(function() {
		var self = this;
		$(self).bind('click', function() {
			Gio.Core.Category.currenAction = this;
			var stringId = $(this).attr('rel');
			var temp = stringId.split('_');
			var act = temp[0];
			var id = temp[1];
			var url = ''; 
			switch (act) {
				case 'delete':
					url = '<?php echo $this->url('category_category_delete'); ?>';
					var messsage_confirm = '<?php echo $this->STRING->escape($this->TRANSLATOR->translator('category_actions_delete_confirm')); ?>';
					break;
				case 'activate':
				case 'deactivate':
					url = '<?php echo $this->url('category_category_activate'); ?>';
					var messsage_confirm = (act == 'activate') 
												? '<?php echo $this->STRING->escape($this->TRANSLATOR->translator('category_actions_activate_confirm')); ?>'
												: '<?php echo $this->STRING->escape($this->TRANSLATOR->translator('category_actions_deactivate_confirm')); ?>';
					break;
			}
			data = { url: url, category_id: id, act: act };
			Gio.Message.yesButton = '<?php echo $this->STRING->escape($this->TRANSLATOR->translator('common_yes_button', 'core')); ?>';
			Gio.Message.noButton = '<?php echo $this->STRING->escape($this->TRANSLATOR->translator('common_no_button', 'core')); ?>';
			Gio.Message.confirm(messsage_confirm, 'Gio.Core.Category.actions', data);
		});
	});
});
</script>